<template>
  <div class="qywz_box">
    <header-comm></header-comm>
    <div class="header_sele" :style="$parent.$parent.collapsed==false?'left: 10%':'left:80px'">
      <div class="add_qy">
        客户名称：
        <a-select
          showSearch
          :allowClear="true"
          :value="khvalue"
          style="width: 200px"
          :defaultActiveFirstOption="false"
          :showArrow="false"
          :filterOption="false"
          @search="khmcSearch"
          @change="khmcChange"
          :notFoundContent="null"
        >

          <a-select-option v-for="(item,i) in dataSource" :key="i" :value="item.GSMC">{{item.GSMC}}</a-select-option>
        </a-select>
        <a-button type="primary" @click="tjwz">添加文章</a-button>
        <a-popconfirm v-if="dataSource.length" title="确认删除吗?" @confirm="() => start()">
          <a href="javascript:;">
            <a-button>批量删除</a-button>
          </a>
        </a-popconfirm>
      </div>
      <div>
        <div class="sele">
          <a-input placeholder="请输入客户名称" style="width: 200px" v-model="gsmc" @change="gsmcSearch" />
          <a-icon type="search" class="search" />
        </div>
        <div class="list_head">
          <div class="fl_right">
            <div class="sele">
              <a-input
                placeholder="请输入分类"
                style="width: 200px"
                v-model="articleClass"
                @change="flChange"
              />
              <a-icon type="search" class="search" />
            </div>

           
          </div>
        </div>
        <div class="gl_tjwz" v-if="$store.state.tjwz">
          <tjwzym class="gl_tjym"></tjwzym>
        </div>
        <div class="seach">
          <div class="sele">
            <a-input placeholder="请输入文章标题 " style="width: 250px" @change="onSearch" />
            <a-icon type="search" class="search" />
          </div>
        </div>
      </div>
    </div>
    <!-- 表格-================= -->
    <div class="bcg">
      <div class="glwz">
        <!-- <xzl v-if="$store.state.xzlflg"></xzl> -->
        <a-table
          @change="handleChangelist"
          :loading="loading"
          :columns="columns"
          :pagination="pagination"
          :dataSource="data"
          :rowSelection="rowSelection"
        >
          <template slot="pic" slot-scope="text, record, index">
            <div>
              <img
                class="qywzpic"
                :src="`https://image.bearing.cn/qy/member/`+record.sqdwid+'/'+record.pic"
                alt
              />
            </div>
          </template>

          <template slot="operation" slot-scope="text, record, index">
            <div class="editable-row-operations">
              <a class="del" @click="()=>xiugai(record,record.key)">修改</a>
              <a-popconfirm class="del" title="确认删除吗?" @confirm="() => wzdelete(record,record.key)">
                <a href="javascript:;">删除</a>
              </a-popconfirm>
            </div>
          </template>
        </a-table>
      </div>
    </div>
   
  </div>
</template>

<script>
import qs from "qs";
import headerComm from "../header-comm/header.vue";
import tjwzym from "../tjwz/tjwzym.vue";
// import xzl from "./xzl.vue";

const columns = [
  {
    title: "编号",
    dataIndex: "id_num"
  },
  {
    title: "企业ID",
    dataIndex: "sqdwid"
  },
  {
    title: "客户名称",
    dataIndex: "gsmc"
  },
  {
    title: "图片",
    dataIndex: "pic",
    scopedSlots: { customRender: "pic" }
  },
  {
    title: "分类",
    dataIndex: "articleName"
  },

  {
    title: "标题",
    dataIndex: "title"
  },
  {
    title: "发布时间",
    dataIndex: "datetime"
  },
  {
    title: "操作",
    dataIndex: "operation",
    scopedSlots: { customRender: "operation" }
  }
];
//上传图片
function getBase64(img, callback) {
  const reader = new FileReader();
  let file = [];
  file = reader;
  reader.addEventListener("load", () => callback(reader.result));
  reader.readAsDataURL(img);
  sessionStorage.setItem("file", reader);
}
const data = [];
export default {
  inject: ["reload"],
  data() {
    this.cacheData = data.map(item => ({ ...item }));
    return {
      data,
      columns,
      sele: "",
      serchvalue: "",
      pagination: {
        defaultPageSize: 10,
        showTotal: total => `共 ${total} 条数据`,
        total: 0
      },
      loading: true,
      datakh: [],
      dataSource: [],
      datakhId: "",
      khvalue: "",
      articleClass: "",
      selectedRowKeys: [],
      fyflag: false,
     
      fldata: [],
      gsmc: ""

    };
  },
  components: {
    headerComm,
    tjwzym
    // xzl
  },
  methods: {
    //搜索框
    onSearch(e) {
      this.serchvalue = e.target.value;
      this.handleChangelist(this.pagination);
    },
    gsmcSearch(e) {
      this.gsmc = e.target.value;
      this.handleChangelist(this.pagination);
    },
    //表格
    handleChangelist(pagination) {

     
      this.axios
        .post(
          "wenzhang/AdminArticleList.php",
          qs.stringify({
            page: pagination.current,
            pageSize: pagination.defaultPageSize,
            sqdwid: "",
            title: this.serchvalue,
            gsmc: this.gsmc,
            articleClass: this.articleClass
          })
        )
        .then(res => {
     
          if (res.data.code == 303) {
            this.data = res.data.result.data;
            this.pagination.total = Number(res.data.result.total);
            this.loading = false;
            this.cacheData = this.data.map(item => ({
              ...item
            }));
            for (var i = 0; i < res.data.result.data.length; i++) {
              this.data[i].key = res.data.result.data[i].id_num;
            }
          } else {
            this.loading = false;
            this.data = [];
          }
        });
      
    },

    //删除客户列表
    wzdelete(value, key) {
     
      const dataSource = [...this.data];
      this.data = dataSource.filter(item => item.key !== key);
      this.axios
        .post(
          "wenzhang/AdminArticleListDelete.php",
          qs.stringify({ id_num: value.id_num })
        )
        .then(res => {
          console.log(res);
          if (res.data.code == 303) {
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg
            });
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg
            });
          }
        });
    },
    start() {
      this.loading = true;
      const newData = [...this.data];
      let selectedRowKeys = this.selectedRowKeys;
     
      this.axios
        .post(
          "wenzhang/AdminArticleListBatchDelete.php",
          qs.stringify({
            id_num: selectedRowKeys
          })
        )
        .then(res => {
        
          //更新数据成功
          if (res.data.code == 303) {
            this.loading = false;
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg,
              duration: 1.5
            });
            selectedRowKeys.map(rowkeys => {
              this.data = this.data.filter(item => item.key !== rowkeys);
            });
          } else {
            this.loading = false;

            this.$notification["error"]({
              message: "失败",
              description: res.data.msg,
              duration: 1.5
            });
          }
        });
    },
    
    handleChange(value) {
      console.log(`selected ${value}`);
    },
    tjwz() {
      if (this.khvalue != "" && this.khvalue != undefined) {
        this.$store.state.tjwz = true;
        this.$store.state.glxgsqdwid = this.datakhId;
        console.log(this.$store.state.glxgsqdwid);
      } else {
        this.$notification["error"]({
          message: "失败",
          description: "请先选择客户名称",
          duration: 1.5
        });
      }
    },
    
    xiugai(value, key) {
     
      this.$store.state.id_num = value.id_num;
      this.$store.state.title = value.title;
      this.$store.state.content = value.content;
      this.$store.state.articleClass = value.articleClass;
      this.$store.state.pushTime = value.datetime;
      this.$store.state.source = value.source;
      this.$store.state.author = value.author;
      this.$store.state.articleRemark = value.articleRemark;
      this.$store.state.picList = Number(value.picList);
      this.$store.state.browserTitle = value.browserTitle;
      this.$store.state.browserKeyword = value.browserKeyword;
      this.$store.state.browserDescribe = value.browserDescribe;
      this.$store.state.annex = value.annex;
      this.$store.state.glxgsqdwid = value.sqdwid;
      this.$store.state.tjwzimgUrl = value.pic;

      this.$store.state.tjwz = true;
      this.$store.state.xiugaiflag = "123";
    },
    //文章分类列表
   
    flChange(e) {
      this.articleClass = e.target.value;
      
      this.handleChangelist(
        this.pagination
      );
    },
    
    //企业文章列表
    //客户名称
    flfilterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toUpperCase()
          .indexOf(input.toUpperCase()) >= 0
      );
    },
    //客户名称
    getkhmc(value) {
      this.axios
        .post("yangben/SelectYbGsmc.php", qs.stringify({ gsmc: value }))
        .then(res => {
          console.log(res);
          if (res.data.code == 303) {
            this.dataSource = res.data.result;
            if (res.data.result.length == 0) {
              this.dataSource = [];
            }
          }
        });
    },
    //客户名称查询
    khmcChange(value) {
      this.khvalue = value;
      this.$store.state.qywzgsmc = this.khvalue;

      for (var i = 0; i < this.dataSource.length; i++) {
        if (this.khvalue == this.dataSource[i].GSMC) {
          this.datakhId = this.dataSource[i].SQDWID;
          this.$store.state.qywzkhid = this.datakhId;
         
        }
      }
      if (this.khvalue == "") {
        this.datakhId = "";
        this.$store.state.qywzkhid = "";
        this.$store.state.qywzgsmc = "";
      }
      if (this.khvalue == undefined) {
        this.$store.state.glxgsqdwid = "";
        this.getkhmc();
      }
    },
    khmcSearch(value) {
      this.getkhmc(value);
    }
  },
  computed: {
    rowSelection() {
      const { selectedRowKeys } = this;

      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(
            `selectedRowKeys: ${selectedRowKeys}`,
            "selectedRows: ",
            selectedRows
          );
          this.selectedRowKeys = selectedRowKeys;
          console.log(this.selectedRowKeys);
        }
      };
    }
  },

  created() {
    this.handleChangelist(this.pagination);
    this.$store.commit("getwzfl");
    this.getkhmc();
  }
};
</script>

<style lang='less' scoped>
.qywz_box {
  .gl_tjwz {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999999999;
    height: 930px;
    .gl_tjym {
      position: absolute;
      width: 100%;
      left: 50%;
      transform: translateX(-50%);
    }
  }
  
  .header_sele {
    position: absolute;
    top: 55px;
    left: 10%!important;
    z-index: 99;
    text-align: right;
    width: 98%;
    background-color: #fff;
    box-shadow: 5px 5px 5px #ccc;
    // height: 50px;
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .add_qy {
      // width: 50%;
      display: inline-block;
      text-align: left;
      padding: 10px 0;
      height: 50px !important;
      button {
        margin: 0 10px;
      }
      .ant-btn-primary {
        background-color: #1890ff !important;
        border: 0 !important;
      }
    }
    .seach {
      display: inline-block;
      
    }
    .sele {
        position: relative;
      display: inline-block;

        i {
          position: absolute;
          top: 10px;
          right: 8px;
        }
      }
    .list_head {
      display: inline-block;
      .tit {
        font-size: 16px;
      }
      padding: 10px 15px;
      .fl_right {
        .margin {
          margin: 0 5px;
        }
        .xzl_btn:hover {
          border: 0 !important;
          color: #fff !important;
          background: #1890ff !important;
        }
      }
    }
  }
  .glwz {
    width: 98%;
    position: absolute;
    top: 147px;
    left: 1%;
    background-color: #fff;
    border: 1px solid #ccc;
    .ant-table-thead > tr,
    .ant-table-tbody > tr {
      height: 50px !important;
    }
    .del {
    }
    .qywzpic {
      width: 50px;
      height: 50px;
    }
  }

  .fyloading {
    position: absolute;
    width: 90% !important;
    border: 1px solid #91d5ff;
    background: rgba(255, 255, 255, 0.5);
    padding: 30px;
    height: 800px !important;
    z-index: 999999 !important;
    top: 108px;
    left: 201px;
    // opacity: .5!important;
    .ant-spin-blur {
      opacity: 1 !important;
    }
    .ant-spin-text {
      font-size: 18px !important;
    }
    // .spin-content{
    //   color: #000;
    //   font-size: 16px!important;
    //   top: 100px!important;
    // }
  }
  .ant-spin-nested-loading > div > .ant-spin {
    top: 100px !important;
  }
  .del {
    padding-right: 15px;
    color: #40a9ff;
    cursor: pointer;
  }
}
</style>
